package com.aim.questionnaire.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import org.apache.catalina.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.aim.questionnaire.beans.HttpResponseEntity;
import com.aim.questionnaire.common.Constans;
import com.aim.questionnaire.dao.UserEntityMapper;
import com.aim.questionnaire.dao.entity.UserEntity;
import com.aim.questionnaire.service.UserService;
import com.github.pagehelper.PageInfo;


/**
 * Created by wln on 2018\8\9 0009.
 */
@RestController
@RequestMapping("/admin")
public class UserController {

  private final Logger logger = LoggerFactory.getLogger(UserController.class);

  @Autowired
  private UserService userService;

  /**
   * 用户登录
   *
   * @param userEntity
   * @return
   */
  @RequestMapping(value = "/userLogins", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity userLogin(@RequestBody UserEntity userEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      List<UserEntity> hasUser = userService.selectUserInfo(userEntity);
      if (CollectionUtils.isEmpty(hasUser)) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.LOGIN_USERNAME_PASSWORD_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(userEntity);
        httpResponseEntity.setMessage(Constans.LOGIN_MESSAGE);
      }

    } catch (Exception e) {
      logger.info("userLogin 用户登录>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 查询用户列表（模糊搜索）
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/queryUserList", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity queryUserList(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    List<UserEntity> userEntityList = new ArrayList<>();
    try {
      PageInfo pageInfo = userService.queryUserList(map);
      if (pageInfo.getList().size() == 0) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(pageInfo);
        httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
      }

    } catch (Exception e) {
//logger.info("userLogin 用户登录>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 创建用户的基本信息
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/addUserInfo", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity addUserInfo(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.addUserInfo(map);
      if (result == 3) {
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 创建用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 编辑用户的基本信息
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/modifyUserInfo", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity modifyUserInfo(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.modifyUserInfo(map);
      if (result == 3) {
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 编辑用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }


  /**
   * 根据用户id查询用户基本信息
   *
   * @param userEntity
   * @return
   */
  @RequestMapping(value = "/selectUserInfoById", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity selectUserInfoById(@RequestBody UserEntity userEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      Map<String, Object> result = userService.selectUserInfoById(userEntity);
      if (result.get("id").equals("")) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.LOGIN_USERNAME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(result);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 通过ID查询用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }


  /**
   * 修改用户状态
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/modifyUserStatus", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity modifyUserStatus(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.modifyUserStatus(map);
      if (result != 1) {
        //这种情况不存在 但为了应对将来出现管理员权限种类增加的情况需要加上这种判断
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.UPDATE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("deleteUserInfo 更新用户的权限>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 删除用户信息
   *
   * @param userEntity
   * @return
   */
  @RequestMapping(value = "/deleteUserInfoById", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity deteleUserInfoById(@RequestBody UserEntity userEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.deteleUserInfoById(userEntity);
      if (result == 3) {
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("deleteUserInfo 删除用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 用户没有权限
   *
   * @return
   */
  @RequestMapping(value = "/error")
  public HttpResponseEntity logout() {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    return httpResponseEntity;
  }
}
